package com.zhangyj.applets.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.Border;

import com.zhangyj.applets.icons.Icons;

public class JSelectTextField extends JPanel {
	
	private static final long serialVersionUID = 1001737811436418958L;
	public JTextField textfield;
	public JLabel label;
        public JSelectTextField() {
		this.setLayout(new BorderLayout());
		textfield = new JTextField();
		Border border = textfield.getBorder();
		textfield.setBorder(BorderFactory.createEmptyBorder());
		label = new SelectLabel();
		this.add(textfield, BorderLayout.CENTER);
		this.add(label, BorderLayout.EAST);
		this.setBackground(textfield.getBackground());
		this.setBorder(border);
	}
	public JSelectTextField(int num) {
		this.setLayout(new BorderLayout());
		textfield = new JTextField();
		textfield.setColumns(num);
		Border border = textfield.getBorder();
		textfield.setBorder(BorderFactory.createEmptyBorder());
		label = new SelectLabel();
		this.add(textfield, BorderLayout.CENTER);
		this.add(label, BorderLayout.EAST);
		this.setBackground(textfield.getBackground());
		this.setBorder(border);
	}

	static class SelectLabel extends JLabel implements MouseListener{
		private static final long serialVersionUID = 4016903738245179981L;
		Date date;
		Image img;
		boolean isToday = false;

		public SelectLabel() {
			setHorizontalAlignment(JLabel.CENTER);
			setPreferredSize(new Dimension(20, 20));
			setToolTipText("Search");
			addMouseListener(this);
			this.setIcon(Icons.search1);
			
		}

		public void mousePressed(MouseEvent e) {
			this.setIcon(Icons.search2);
		}

		public void mouseClicked(MouseEvent e) {
			JDialog jd = new JBoastDialog();
			jd.setTitle("Selecor");
			jd.setModal(true);
			jd.setSize(400, 300);
			jd.setVisible(true);
			this.setIcon(Icons.search1);
		}

		public void mouseReleased(MouseEvent e) {
			if (isEnabled()) {
				this.setIcon(Icons.search2);
			}
		
		}

		public void mouseEntered(MouseEvent e) {
			if (isEnabled()) {
				this.setIcon(Icons.search2);
			}
		}

		public void mouseExited(MouseEvent e) {
			if (isEnabled()) {
				this.setIcon(Icons.search1);
			}
		}

		public void paint(Graphics g) {
			super.paint(g);
			
		}
	
	}
	public static void main(String[] args) {
//		String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
//		// String metal="javax.swing.plaf.metal.MetalLookAndFeel";
//		// String motif="com.sun.java.swing.plaf.motif.MotifLookAndFeel";
//		try {
//			UIManager.setLookAndFeel(windows);
//			// SwingUtilities.updateComponentTreeUI(this);
//		} catch (Exception e) {
//			System.out.println("不能设置这个界面风格:" + e);
//		}
		JFrame f = new JFrame("JDatePicker Demo");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel c = new JPanel();

		c.add(new JSelectTextField(20));
		JDatePicker datePickerFrom = new JDatePicker();
		c.add(datePickerFrom);
		c.add(new JLabel("To:"));
		JDatePicker datePickerTo = new JDatePicker();
		datePickerTo.getSelectedItem();
		datePickerTo.setEditable(false);
		c.add(datePickerTo);
		f.getContentPane().add(c, BorderLayout.NORTH);

		f.getContentPane().add(new JDatePicker(), BorderLayout.SOUTH);

		final JTable table = new JTable(20, 10);
		JComboBox editor = new JDatePicker();
		editor.setBorder(null);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setDefaultEditor(Object.class, new DefaultCellEditor(editor));
		JScrollPane sp = new JScrollPane(table);
		f.getContentPane().add(sp, BorderLayout.CENTER);

		f.setSize(600, 400);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		f.setLocation((screenSize.width - f.getWidth()) / 2, (screenSize.height - f.getHeight()) / 2);
		f.setVisible(true);
	}
}
